<?php
/**
 * @Author:      lyt
 * @DateTime:    2016-11-21 14:37:58
 * @Description: 用于奖项组的 控制其
 */
class MktAwardController extends BaseController {
    
    /**
     * @Author:      lyt
     * @DateTime:    2016-11-21 14:47:18
     * @Description: 奖项 组的列表
     */
    public  function lists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "page"=>'integer',
                "page_size"=>'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            $activites = ActivityAwardGroup::with("created_byname")->orderBy('id','desc');
            if(Input::get("status")!=""){
                $activites->where("status",Input::get("status"));
            }
          /*  if(Input::get("begin_date")){
                $activites->whereRaw('created_at >= '."'".Input::get("begin_date")."'");
            }*/
            if(Input::get("begin_date")){
                $activites->whereBetween('created_at',[Input::get('begin_date').' '.'00:00:00',Input::get('begin_date').' '.'23:59:59']);
            }
            if(Input::get("end_date")){
                $activites->whereRaw('created_at <='."'".Input::get("end_date")."'");
            }
            if(Input::get("name")!=""){
                $activites->where('name', 'LIKE', '%'.Input::get('name').'%');
            }

            $count=$activites->count();
            $activites=$activites->skip($page_size*($page-1))->take($page_size)->get();
            $arr_activites = array();
            foreach ($activites as $_a) {
                $activity_coupon_info  = ActivityAward::where('award_group_id',$_a->id)->get();
                foreach($activity_coupon_info as $v){
                    $probability = $v->probability*100;
                    $v->probability = $probability.'%';
                }
                $arr_activites[]=array(
                    "id"=>$_a->id,
                    "name"=>$_a->name,
                    "status"=> ActivityAwardGroup::$status[$_a->status],
                    "created_byname"=>isset($_a->created_byname)?$_a->created_byname->real_name:'',
                    "updated_at"=>$_a->updated_at."",
                    "created_at"=>$_a->created_at."",
                    "awards_count"=>sizeof($activity_coupon_info),
                    "awards"=>$activity_coupon_info,
                );
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"award_groups"=>$arr_activites));
        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      kc
     * @DateTime:    2016-11-21 17:34:18
     * @Description: 奖项的添加
     */
    public function prize_create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'name' => 'required',
                'status' => 'required',
                'prize' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }
            $activity_award_group = ActivityAwardGroup::PrizeCreate(Input::all(),$mkt_user->id,$response);
            if(is_null($activity_award_group)){
                break;
            }
        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "prize_create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:    2016-11-22 15:12:18
     * @Description: 奖项的编辑
     */
    public function update(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => "required",
                'id' => "required",
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }
            $award_group = ActivityBaseInfo::where('award_group',Input::get('id'))->where('begin_date','<',date("Y-m-d H:i:s",time()))->first();
            if(count($award_group) > 0 && ( strtotime($award_group->end_date) < time())){
                $response = Lang::get('mkt_error.mkt_award_edit_error_610003');
                break;
            }
            try{
                $activity_award_group = ActivityAwardGroup::find(Input::get('id'));
                if(Input::get('name')){
                    $activity_award_group->name = Input::get('name');
                }
                $activity_award_group->status = Input::get('status');
                $activity_award_group->created_by = $mkt_user->id;
                $activity_award_group->save();
                if(Input::get('prize')){
                    $data = json_decode(Input::get('prize'),true);
                    foreach($data as $v) {
                       if(isset($v['id'])){
                           $activity_award =ActivityAward::find($v['id']);
                           if ($v['name']) {
                               $activity_award->name = $v['name'];
                           }
                           if ($v['award_group_id']) {
                               $activity_award->award_group_id = Input::get('id');
                           }
                           if ($v['max_number']) {
                               $activity_award->max_number = $v['max_number'];
                           }
                           if ($v['probability']) {
                               $activity_award->probability = bcdiv(trim($v['probability'],'%'), 100,3);
                           }
                           if ($v['prize_name']) {
                               $activity_award->prize_name = $v['prize_name'];
                           }
                           if (!empty($v['prize_id'])) {
                               $activity_award->prize_id = $v['prize_id'];
                           }
                           $activity_award->prize_type = 0;
                           $activity_award->created_by = $mkt_user->id;
                           $activity_award->save();
                       }else{
                           if(empty($v['name']) || empty($v['max_number']) || empty($v['probability']) || empty($v['prize_name']) || empty($v['prize_id'])){
                               $response = Lang::get("mkt_error.args_error_100000");
                               $response["result"]=$validator->messages()->all();
                               break;
                           }
                           $activity_award = new ActivityAward();
                           $activity_award->name = $v['name'];
                           $activity_award->award_group_id = Input::get('id');
                           $activity_award->max_number = $v['max_number'];
                           $activity_award->probability = bcdiv($v['probability'], 100, 2);
                           $activity_award->prize_name = $v['prize_name'];
                           $activity_award->prize_id = $v['prize_id'];
                           $activity_award->prize_type = 0;
                           $activity_award->created_by = $mkt_user->id;
                           $activity_award->save();
                        }
                    }
                 }
            $response = array(
                "code"=> 0,
                "msg" => "ok",
            );
            }catch (Exception $e){
                Log::error("error", array("system" => "mkt" ,"event" => "update", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("mkt_error.mkt_award_save_error_610002");
                break;
            }
        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "update", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:    2016-11-24 11:00:18
     * @Description: 奖项的详情
     */
    public function details(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => "required",
                'id' => "required",
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }
            $activites = ActivityAwardGroup::with("award")->where('id',Input::get('id'))->first();
            foreach(isset($activites->award)?$activites->award:array() as $v){
                $probability = $v->probability*100;
                $v->probability = $probability.'%';
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $activites);
        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "details", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}